package com.code.provider.proxy;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author lyf
 * @date 2021-10-09 21:08
 */
public class RpcProxyServer {

    public void publish(Object service, int port) throws Exception {
        ExecutorService executorService = Executors.newCachedThreadPool();

        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(port);
            // 服务端接收请求，在异步处理
            while (true) {
                Socket socket = serverSocket.accept();
                executorService.submit(new ProcessorHandler(socket, service));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (serverSocket != null) {
                serverSocket.close();
            }
        }
    }
}
